package com.kly.cashmall.module.web;

import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;

/**
 * Created by  on 2017/8/23.
 */
class ProgressWebChrome extends WebChromeClient {
    private int currProgress = -1;
    private Handler mHandler = new Handler();
    private ProgressBar progressbar;

    ProgressWebChrome(WebView webView, ProgressBar progressbar) {
        this.progressbar = progressbar;
        this.mWebView = webView;
    }

    private Runnable runnable = new Runnable() {
        public void run() {
            if (currProgress < 80) {
                progressbar.setProgress(currProgress);
                currProgress++;
                mHandler.postDelayed(this, currProgress < 20 ? 40 : 2 * currProgress);
            } else {
                if (currProgress == 100) {
                    AlphaAnimation animation = new AlphaAnimation(1, 0);
                    animation.setDuration(250);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        public void onAnimationEnd(Animation animation) {
                            progressbar.setVisibility(View.GONE);
                        }

                        public void onAnimationRepeat(Animation animation) {
                        }

                        public void onAnimationStart(Animation animation) {
                        }
                    });
                    progressbar.startAnimation(animation);
                } else {
                    mHandler.postDelayed(this, 10);
                }
            }
        }
    };

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if (currProgress < 0) {
            currProgress = 0;
            if (progressbar.getVisibility() == View.GONE
                    || progressbar.getVisibility() == View.INVISIBLE) {
                progressbar.setVisibility(View.VISIBLE);
            }
            mHandler.postDelayed(runnable, 200);
        }
        currProgress = 100 - (int) ((100 - currProgress) * (1.0 - newProgress / 100.0));
        progressbar.setProgress(currProgress);
        super.onProgressChanged(view, newProgress);
    }

    private View myView = null;
    private WebView mWebView;

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        ViewGroup parent = (ViewGroup) mWebView.getParent();
        parent.removeView(mWebView);
        parent.addView(view);
        myView = view;
        myView.setBackgroundColor(Color.BLACK);
    }

    @Override
    public void onHideCustomView() {
        if (myView != null) {
            mHandler.removeCallbacksAndMessages(null);
            ViewGroup parent = (ViewGroup) myView.getParent();
            parent.removeView(myView);
            parent.addView(mWebView);
            myView = null;
        }
    }
}
